<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.ReceiveorderDAO"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@page import="db.SubstoreDAO"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.Receivepurmat"%>
<%
	WebApplicationContext ctx = WebApplicationContextUtils
			.getWebApplicationContext(getServletContext());
	ReceiveorderDAO receiveorderDao = (ReceiveorderDAO) ctx
			.getBean("ReceiveorderDAO");
	ReceivepurmatDAO receivepurmatDao = (ReceivepurmatDAO) ctx
			.getBean("ReceivepurmatDAO");
	SubstoreDAO substoreDao = (SubstoreDAO) ctx.getBean("SubstoreDAO");
	ProjectDAO projectDao = (ProjectDAO) ctx.getBean("ProjectDAO");
	
	String[] indexs = request.getParameterValues("indexs");
	String[] matIds = request.getParameterValues("matIds");
	String[] numbers = request.getParameterValues("numbers");
	String[] substores = request.getParameterValues("substores");
	String[] projects = request.getParameterValues("projects");
	
	if(indexs != null)
	for(int i=0; i<indexs.length; i++){
		 int index = Integer.parseInt(indexs[i]);
		 int matId = Integer.parseInt(matIds[index]);
		 int number = Integer.parseInt(numbers[index]);
		 int substore = Integer.parseInt(substores[index]);
		 Receivepurmat receivepurmat = receivepurmatDao.findById(matId);
		 
		 if(receivepurmat.getPurNum() == receivepurmat.getUseNum() && (receivepurmat.getPurNum() - number) == 0)
			 receivepurmat.setIsDelete(1);
		 
		 receivepurmat.setPurNum(receivepurmat.getPurNum() - number);
		 receivepurmat.setUseNum(receivepurmat.getUseNum() - number);
		 receivepurmatDao.attachDirty(receivepurmat);
		 
		 receivepurmat.setImportNum(number);
		 receivepurmat.setDate(new Date());
		 receivepurmat.setPurNum(number);
		 receivepurmat.setUseNum(number);
		 receivepurmat.setSubStore(substore);
		 //TODO 专项
		 if(substore == 1){
		 	 receivepurmat.setProCode(projects[index]);
		 }else{
			 receivepurmat.setProCode("");
		 }
		 receivepurmatDao.save(receivepurmat);
	}
	
	request.setAttribute("info","success");
	pageContext.forward("/vm/transfer/stockTransfer.vm");
%>